<?php

	####################################################
	# ini                                              #
	####################################################
	# 헤더
	header("Content-Type: text/html; charset=UTF-8");

	# 타임존 셋
	date_default_timezone_set('Asia/Seoul');

	# 세션 셋
	session_set_cookie_params(0,'/','naiyumie.com');
	session_start();
	//print_r(session_get_cookie_params());

	# ob_start
	ob_start('ob_gzhandler');

	# 퍼포먼스 체크
	list($usec, $sec) = explode(" ", microtime());
	$elapsed_start = ((float)$usec + (float)$sec);


	####################################################
	# application configration                         #
	####################################################
	# application domain
	define('DOMAIN',            'naiyumie.com');

	# application title
	define('PROJECT_NAME',		'ggoggo_framework');
	define('TITLE',             '꼬꼬 프레임워크');

	# path
	define('PROJECT_DIR',		'/');
	define('CLASSES_DIR',		'classes'.'/');
	define('NLOG',				PROJECT_DIR.'nlog'.'/'.'nlog.log');
	define('PAGE_ERROR',		PROJECT_DIR.'error');
	define('JS_ASSETS_DIR',		PROJECT_DIR.'js'.'/');
	define('CSS_ASSETS_DIR',	PROJECT_DIR.'css'.'/');
	define('IMG_ASSETS_DIR',	PROJECT_DIR.'img'.'/');

	# DB
	define('MYSQL_HOST',		'localhost');
	define('MYSQL_USER',		'root');
	define('MYSQL_PASS',		'apmsetup');
	define('MYSQL_NAME',		'ggoggo_framework');

	# 기본 패스
	$DEFAULT_PATH = $_SERVER["DOCUMENT_ROOT"].PROJECT_DIR;

	# basename($_SERVER['PHP_SELF'])
	$PHP_SELF = substr(basename($_SERVER['PHP_SELF']), 0, -4);
	$tmpmode = explode('.', $PHP_SELF);
	$PHP_SELF_ARR['MODE'] = $tmpmode[0];
	$PHP_SELF_ARR['PACKAGE'] = $tmpmode[1];
	$PHP_SELF_ARR['FILE'] = $tmpmode[2];
	//print_r($tmpmode);
	//print_r($PHP_SELF_ARR);

	# 어플리케이션 에셋 디렉터리.
	$JS = JS_ASSETS_DIR;
	$CSS = CSS_ASSETS_DIR;
	$IMG = IMG_ASSETS_DIR;
	$VersionTime = "?t=".@date("Ymdhis");

	# 기본 함수를 임포트.
	include_once( $DEFAULT_PATH.'functions.php' );


	# 개발 모드 인지 출시 모드 인지 결정
	if(!empty($_COOKIE['appmode']) && $_COOKIE['appmode'] == 'dev') {
		# 개발 모드 이면
		define('APPMODE', "dev");
	} else {
		# 출시 모드 이면
		define('APPMODE', "product");
	}

	# 에러 리포팅
	error_reporting_handler();
	set_error_handler("error_listener");

	####################################################
	# library import and object instance.              #
	####################################################

	# 인클루드 파일.
	$incs = class_autoload();

	# 모든 인클루드 된 파일 보기.
	//Nlog::puts('included file',$incs);
	//echo sprintf("<xmp>%s</xmp>", print_r(get_included_files(), true));


	# object instance
	$html			= new Xhtml();
	$db				= new Nmysql();
	$dbsess			= new DBSession($db);
	$tplt			= new Template();
	$sec			= new Security($db);
	$appm			= new ApplicationManager($db);
	$sidebar		= new Sidebar();
	$epnt			= new EntryPoint($db);
	$auth			= new Auth($db, $dbsess);



	####################################################
	# referer check and security                       #
	####################################################

	# process referer check
	if(strstr("process",$PHP_SELF_ARR['FILE']) != FALSE){
		Request::referer();
	}

	# ipfilltering check
	$sec->run_ipfilltering_mode($PHP_SELF_ARR['MODE'], $_SERVER['REMOTE_ADDR']);



	####################################################
	# entry poin >> dynamic path constants             #
	####################################################

	# ADMIN_INDEX_FILE >> entry point 가 있는지 검사한다. 없다면 부트캠프가 디폴트가 된다.
	$gip = $epnt->get_init_packages('admin');
	if($gip['has']){
		define('ADMIN_INDEX_FILE', PROJECT_DIR.$gip['res']['file']);
	} else {
		define('ADMIN_INDEX_FILE', PROJECT_DIR.'admin.bootcamp.index');
	}
	# INDEX_FILE >> entry point 가 있는지 검사한다. 없다면 index가 디폴트가 된다.
	$gip = $epnt->get_init_packages('users');
	if($gip['has']){
		define('INDEX_FILE', PROJECT_DIR.$gip['res']['file']);
	} else {
		define('INDEX_FILE', PROJECT_DIR.'index');
	}
	//Nlog::puts(ADMIN_INDEX_FILE);
	//Nlog::puts(INDEX_FILE);


	####################################################
	# request data($_POST AND $_GET FROM $_REQUEST)    #
	####################################################
	# REQUEST(get and post variables)

	# GET(url variables)

	# POST(form variables)

	# Validation(special case validate)

	####################################################
	# implementation                                   #
	####################################################

	# 세션 정보
	$sess = $dbsess->sess_get();

	# 세션 값에따른 옵저버
	$sessob = $epnt->observer($sess);
	//Nlog::puts($sessob);

	####################################################
	# 네비게이션 활성화 및 타이틀 설정                 #
	####################################################

	# 관리자 모드일 경우
	if(strstr("admin",$PHP_SELF_ARR['MODE']) != FALSE){
		# 패키지키
		$packarr = explode('.', basename($_SERVER['PHP_SELF']));
		$retnavi = $db->get($quenavi = sprintf("SELECT `packages_name` FROM `ggo_cfg_packages_pref` WHERE `packages_key` = '%s';", $packarr[1]));
		$pkname = $retnavi['res']['packages_name'];

		# 패키지
		$retnavi = $db->get($quenavi = sprintf("SELECT `package_filename`, `package_desc` FROM `ggo_cfg_packages` WHERE `package_file` LIKE '%%%s%%';", $PHP_SELF));
		$pname = $retnavi['res']['package_filename'];
		$pdesc = $retnavi['res']['package_desc'];

		# 로컬 네비게이션
		$packnavi = $appm->get_packages_key(0, $db->getcnts('ggo_cfg_packages_pref'));

		# 네비게이션
		$NAVI = $appm->navi_tree();
		//$NAVI_CATEGORY;
		//Nlog::puts('navi', $NAVI);
		//Nlog::puts('name',$packarr);
		//Nlog::puts('retnavi',$retnavi);
		//Nlog::puts('pkname', $pkname);
		//Nlog::puts('packnavi', $packnavi);
	}
?>